package com.skhu.as.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.skhu.as.command.FaultHandlingRegCmd;
import com.skhu.as.domain.DutyHours;
import com.skhu.as.domain.FaultHandling;
import com.skhu.as.domain.ProcessStatus;
import com.skhu.as.domain.RequestType;
import com.skhu.as.domain.Requester;
import com.skhu.as.domain.Worker;
import com.skhu.as.service.FaultHandlingS;
import com.skhu.as.util.DateU;

public class FaultHandlingModC extends SimpleFormController {

	private FaultHandlingS faultHandlingS;
	
	public void setFaultHandlingS(FaultHandlingS faultHandlingS) {
		this.faultHandlingS = faultHandlingS;
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		
		int faultHandlingNo = ServletRequestUtils.getIntParameter(request, "faultHandlingNo", 0);
		FaultHandling faultHandling = faultHandlingS.getFaultHandlingByNo(faultHandlingNo);
		Requester requester = faultHandlingS.getRequesterByNo(faultHandling.getRequesterNo());
		
		FaultHandlingRegCmd faultHandlingRegCmd = new FaultHandlingRegCmd();

		faultHandlingRegCmd.setDutyHoursNo(faultHandling.getDutyHoursNo());
		faultHandlingRegCmd.setFirstWorkerNo(faultHandling.getFirstWorkerNo());
		faultHandlingRegCmd.setProcessYear(faultHandling.getProcessDate().substring(0, 4));
		faultHandlingRegCmd.setProcessMonth(faultHandling.getProcessDate().substring(4, 6));
		faultHandlingRegCmd.setProcessDayOfMonth(faultHandling.getProcessDate().substring(6, 8));
		faultHandlingRegCmd.setProcessingInformation(faultHandling.getProcessingInformation());
		faultHandlingRegCmd.setProcessStatusNo(faultHandling.getProcessStatusNo());
		faultHandlingRegCmd.setReceptionistNo(faultHandling.getReceptionistNo());
		faultHandlingRegCmd.setRequestContent(faultHandling.getRequestContent());
		faultHandlingRegCmd.setRequestTypeNo(faultHandling.getRequestTypeNo());
		
		faultHandlingRegCmd.setRequesterName(requester.getName());
		faultHandlingRegCmd.setRequesterContactNumber(requester.getContactNumber());
		faultHandlingRegCmd.setRequestPlace(requester.getRequestPlace());
		faultHandlingRegCmd.setDepartment(requester.getDepartment());
		
		return faultHandlingRegCmd;
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		int faultHandlingNo = ServletRequestUtils.getIntParameter(request, "faultHandlingNo");
		FaultHandlingRegCmd faultHandlingRegCmd = (FaultHandlingRegCmd)command;
		FaultHandling faultHandling = faultHandlingS.getFaultHandlingByNo(faultHandlingNo);
		Requester requester = faultHandlingS.getRequesterByNo(faultHandling.getRequesterNo());
		
		String month = faultHandlingRegCmd.getProcessMonth();
		String dayOfMonth = faultHandlingRegCmd.getProcessDayOfMonth();
		
		if(month.length() == 1){
			month = "0" + month;
		}
		
		if(dayOfMonth.length() == 1){
			dayOfMonth = "0" + dayOfMonth;
		}
		
		faultHandling.setDutyHoursNo(faultHandlingRegCmd.getDutyHoursNo());
		faultHandling.setFaultHandlingNo(faultHandlingNo);
		faultHandling.setFirstWorkerNo(faultHandlingRegCmd.getFirstWorkerNo());
		faultHandling.setProcessDate(faultHandlingRegCmd.getProcessYear() + "" + month + "" + dayOfMonth);
		faultHandling.setProcessingInformation(faultHandlingRegCmd.getProcessingInformation());
		faultHandling.setProcessStatusNo(faultHandlingRegCmd.getProcessStatusNo());
		faultHandling.setReceptionistNo(faultHandlingRegCmd.getReceptionistNo());
		faultHandling.setRequestContent(faultHandlingRegCmd.getRequestContent());
		faultHandling.setRequesterNo(requester.getRequesterNo());
		faultHandling.setRequestTypeNo(faultHandlingRegCmd.getRequestTypeNo());
		
		requester.setRequesterNo(faultHandlingRegCmd.getRequestTypeNo());
		requester.setContactNumber(faultHandlingRegCmd.getRequesterContactNumber());
		requester.setDepartment(faultHandlingRegCmd.getDepartment());
		requester.setName(faultHandlingRegCmd.getRequesterName());
		requester.setRequestPlace(faultHandlingRegCmd.getRequestPlace());
		
		faultHandlingS.updateFaultHandling(faultHandling);
		faultHandlingS.updateRequester(requester);
		
		return super.onSubmit(request, response, command, errors);
	}
	
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		
		Map ref = new HashMap();
		
		/**
		 * 작업자 목록
		 */
		List<Worker> workerList = faultHandlingS.getWorkerList();
		ref.put("workerList", workerList);
		
		/**
		 * 오늘 날짜 생성
		 */
		DateU dateU = new DateU();
		String today = dateU.getDate();
		ref.put("today", today);
		ref.put("processYear", dateU.getYear());
		ref.put("processMonth", dateU.getMonth());
		ref.put("processDayOfMonth", dateU.getDayOfMonth());
		
		/**
		 * 근무시간
		 */
		List<DutyHours> dutyHoursList = faultHandlingS.getDutyHoursList();
		ref.put("dutyHoursList", dutyHoursList);
		
		/**
		 * 요청구분
		 */
		List<RequestType> requestTypeList = faultHandlingS.getRequestTypeList();
		ref.put("requestTypeList", requestTypeList);
		
		/**
		 * 처리상태
		 */
		List<ProcessStatus> processStatusList = faultHandlingS.getProcessStatusList();
		ref.put("processStatusList", processStatusList);
		
		int faultHandlingNo = ServletRequestUtils.getIntParameter(request, "faultHandlingNo", 0);
		FaultHandling faultHandling = faultHandlingS.getFaultHandlingByNo(faultHandlingNo);
		ref.put("faultHandling", faultHandling);
		
		return ref;
	}
	
}
